package co.yixiang.modules.system.service.impl;

import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.modules.system.domain.TyDeployInfo;
import co.yixiang.modules.system.dto.TyDeployInfoDto;
import co.yixiang.modules.system.dto.queryCriteria.TyDeployInfoQueryCriteria;
import co.yixiang.modules.system.mapper.TyDeployInfoMapper;
import co.yixiang.modules.system.service.TyDeployInfoService;
import co.yixiang.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author DB
* @date 2020-08-22
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "tyDeployInfo")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TyDeployInfoServiceImpl extends BaseServiceImpl<TyDeployInfoMapper, TyDeployInfo> implements TyDeployInfoService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(TyDeployInfoQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TyDeployInfo> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(TyDeployInfoDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<TyDeployInfo> queryAll(TyDeployInfoQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TyDeployInfo.class, criteria));
    }


    @Override
    public void download(List<TyDeployInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TyDeployInfoDto tyDeployInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("渠道编码，对接时团油提供", tyDeployInfo.getChannelId());
            map.put("请求来源系统的名称", tyDeployInfo.getAppKey());
            map.put("秘钥", tyDeployInfo.getAppSecret());
            map.put("查询油站状态和油价", tyDeployInfo.getUrlGetStation());
            map.put("第三⽅⽤户分⻚拉取油站数据列表时调⽤", tyDeployInfo.getUrlGetStations());
            map.put("其他业务接⼝token验证，需先调⽤该接⼝获取token", tyDeployInfo.getUrlLogin());
            map.put("静默登录获取授权码接⼝", tyDeployInfo.getUrlGetSecretCode());
            map.put("订单查询接⼝", tyDeployInfo.getUrlGetOrders());
            map.put("创建时间", tyDeployInfo.getCreateTime());
            map.put("修改时间", tyDeployInfo.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
